﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Xml;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using ScrumTable.Common;
using ScrumTable.Common.Security;
using ScrumTable.Common.Xml;
using ScrumTable.DL.Data.TFS.Properties;

#endregion

namespace ScrumTable.DL.Data.TFS.Services.TeamFoundation
{
    /// <summary>
    /// Wraps the internal TeamFoundationServer instance.
    /// </summary>
    public class TfsWiTypeReader
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly XmlDocument _workItemTypeDefinition;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new TfsServerWrapper instance.
        /// </summary>
        public TfsWiTypeReader(WorkItemType type)
        {
            PreCondition.AssertNotNull(type, "type");

            _workItemTypeDefinition = type.Export(false);

            if (_workItemTypeDefinition == null)
            {
                throw new ArgumentException(string.Format(Resources.ExcWiTypeReaderDefNotFound, type.Name));
            }
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Returns the default field value of the field with the given ref
        /// name.
        /// </summary>
        /// <param name="fieldRefName">Specifies the reference name of the field to retrieve.</param>
        /// <param name="fieldType">Specifies the type of the default field value to retrieve.</param>
        public object GetDefaultFieldValue(string fieldRefName, Type fieldType)
        {
            PreCondition.AssertNotNullOrEmpty(fieldRefName, "fieldRefName");
            PreCondition.AssertNotNull(fieldType, "fieldType");

            XmlNode fieldDefaultNode = _workItemTypeDefinition.DocumentElement.SelectSingleNode("WORKITEMTYPE/FIELDS/FIELD[@refname='" + fieldRefName + "']/DEFAULT");
            if (fieldDefaultNode != null)
            {
                string attributeValue = fieldDefaultNode.GetAttributeValue("value");

                if (attributeValue != null)
                {
                    return attributeValue.ChangeType(fieldType);
                }
            }
            return null;
        }

        /// <summary>
        /// Returns the transition objects stored in the workflow of the current
        /// work item type.
        /// </summary>
        /// <returns>Returns the retrieved transitions.</returns>
        public IEnumerable<Transition> GetWorkflowTransitions()
        {
            if (_workItemTypeDefinition.DocumentElement != null)
            {
                foreach (XmlNode transition in _workItemTypeDefinition.DocumentElement.SelectNodes("WORKITEMTYPE/WORKFLOW/TRANSITIONS/TRANSITION"))
                {
                    yield return new Transition
                                     {
                                         From = transition.GetAttributeValue("from"),
                                         To = transition.GetAttributeValue("to")
                                     };
                }
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion


        //--------------------------------------------------------------------
        // Nested Classes
        //--------------------------------------------------------------------

        /// <summary>
        /// Defines a method to release allocated resources.
        /// </summary>
        public class Transition
        {
            #region Properties
            //--------------------------------------------------------------------
            // Properties
            //--------------------------------------------------------------------

            /// <summary>
            /// Gets the 'from' property content.
            /// </summary>
            public string From { get; internal set; }

            /// <summary>
            /// Gets the 'to' property content.
            /// </summary>
            public string To { get; internal set; }

            #endregion
        }
    }
}